﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data;

public partial class PropertyList : System.Web.UI.UserControl
{
    private SPList propertyList;
    private string propertyID="1";

    protected void Page_Load(object sender, EventArgs e)
    {
        propertyID = HttpContext.Current.Request.QueryString["id"];

        /* used in a test envrionment*/
        /*
        using (SPSite site = new SPSite("http://yu-pc:33333/sites/Home"))
        {
            using (SPWeb web = site.AllWebs[0])
            {
                string listName = "Properties";
                propertyList = web.Lists[listName];
                BindData(propertyList);    
            }
        }
         * */
   
        /* used in sharepoint context */
      
     
        using (SPSite site = new SPSite(SPContext.Current.Site.ID))
        {
            using (SPWeb web = SPControl.GetContextSite(Context).OpenWeb())
            {

                string listName = "Properties";
                propertyList = web.Lists[listName];
                BindData(propertyList); 
            }
        }
     

    }


    protected void SPGridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       
    }


    private DataTable SearchProperties(SPList list)
    {
        SPQuery qry = new SPQuery();

        if (!string.IsNullOrEmpty(propertyID))
        {
            qry.Query = "<Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>" + this.propertyID + "</Value></Eq></Where>";
        }

        else return new DataTable();

        DataTable dataTable = list.GetItems(qry).GetDataTable();

        foreach (DataRow dr in dataTable.Rows)
        {
            dr["Main_x0020_Image"] = dr["Main_x0020_Image"].ToString().Split(',')[0];
        }

        return dataTable;
    }

    private void BindData(SPList propertyList)
    {
        DataTable listItems = SearchProperties(propertyList);
        this.SPGridView1.DataSource = listItems;
        this.SPGridView1.DataBind();
    }
    
}
